const express = require('express');
const auth = require('../../middleware/auth');

const router = express.Router();

// 模拟人员数据
let personnelData = [
  {
    id: '1',
    name: '张三',
    position: '技术总监',
    department: '技术研发部',
    phone: '13800138000',
    email: 'zhangsan@company.com',
    status: '在职',
    joinDate: '2023-09-15',
    workHours: '18:00'
  },
  {
    id: '2',
    name: '李四',
    position: '产品经理',
    department: '产品部',
    phone: '13800138001',
    email: 'lisi@company.com',
    status: '在职',
    joinDate: '2023-08-10'
  },
  {
    id: '3',
    name: '王五',
    position: 'UI设计师',
    department: '设计部',
    phone: '13700137000',
    email: 'wangwu@company.com',
    status: '请假',
    joinDate: '2023-07-20',
    workHours: '18:00'
  },
  {
    id: '4',
    name: '赵六',
    position: '人事专员',
    department: '人事部',
    phone: '13900139000',
    email: 'zhaoliu@company.com',
    status: '在职',
    joinDate: '2023-09-15'
  }
];

// 获取人员列表
router.get('/', auth, async (req, res) => {
  try {
    res.json({
      success: true,
      data: personnelData,
      total: personnelData.length
    });
  } catch (error) {
    console.error('Get personnel error:', error);
    res.status(500).json({ error: '获取人员列表失败' });
  }
});

// 获取单个人员信息
router.get('/:id', auth, async (req, res) => {
  try {
    const { id } = req.params;
    const person = personnelData.find(p => p.id === id);
    
    if (!person) {
      return res.status(404).json({ error: '人员不存在' });
    }
    
    res.json({
      success: true,
      data: person
    });
  } catch (error) {
    console.error('Get person error:', error);
    res.status(500).json({ error: '获取人员信息失败' });
  }
});

// 创建人员
router.post('/', auth, async (req, res) => {
  try {
    const { name, position, department, phone, email, status, joinDate, workHours } = req.body;
    
    // 验证必填字段
    if (!name || !position || !department || !phone || !email) {
      return res.status(400).json({ error: '请填写所有必填字段' });
    }
    
    // 检查邮箱是否已存在
    const existingPerson = personnelData.find(p => p.email === email);
    if (existingPerson) {
      return res.status(400).json({ error: '邮箱已存在' });
    }
    
    const newPerson = {
      id: Date.now().toString(),
      name,
      position,
      department,
      phone,
      email,
      status: status || '在职',
      joinDate: joinDate || new Date().toISOString().split('T')[0],
      workHours
    };
    
    personnelData.push(newPerson);
    
    res.status(201).json({
      success: true,
      data: newPerson,
      message: '人员创建成功'
    });
  } catch (error) {
    console.error('Create person error:', error);
    res.status(500).json({ error: '创建人员失败' });
  }
});

// 更新人员
router.put('/:id', auth, async (req, res) => {
  try {
    const { id } = req.params;
    const { name, position, department, phone, email, status, joinDate, workHours } = req.body;
    
    const personIndex = personnelData.findIndex(p => p.id === id);
    if (personIndex === -1) {
      return res.status(404).json({ error: '人员不存在' });
    }
    
    // 检查邮箱是否被其他人使用
    const existingPerson = personnelData.find(p => p.email === email && p.id !== id);
    if (existingPerson) {
      return res.status(400).json({ error: '邮箱已被其他人使用' });
    }
    
    personnelData[personIndex] = {
      ...personnelData[personIndex],
      name: name || personnelData[personIndex].name,
      position: position || personnelData[personIndex].position,
      department: department || personnelData[personIndex].department,
      phone: phone || personnelData[personIndex].phone,
      email: email || personnelData[personIndex].email,
      status: status || personnelData[personIndex].status,
      joinDate: joinDate || personnelData[personIndex].joinDate,
      workHours: workHours || personnelData[personIndex].workHours
    };
    
    res.json({
      success: true,
      data: personnelData[personIndex],
      message: '人员更新成功'
    });
  } catch (error) {
    console.error('Update person error:', error);
    res.status(500).json({ error: '更新人员失败' });
  }
});

// 删除人员
router.delete('/:id', auth, async (req, res) => {
  try {
    const { id } = req.params;
    
    const personIndex = personnelData.findIndex(p => p.id === id);
    if (personIndex === -1) {
      return res.status(404).json({ error: '人员不存在' });
    }
    
    const deletedPerson = personnelData.splice(personIndex, 1)[0];
    
    res.json({
      success: true,
      data: deletedPerson,
      message: '人员删除成功'
    });
  } catch (error) {
    console.error('Delete person error:', error);
    res.status(500).json({ error: '删除人员失败' });
  }
});

module.exports = router;
